import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/labels/index.dart';
import 'package:im_flutter_sdk_example/jsonserialize/expert/home_data.dart';
import 'package:im_flutter_sdk_example/utils/constants.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:im_flutter_sdk_example/utils/screen.dart';

class Expert extends StatelessWidget {
  final HomeExpertData item;
  const Expert({Key key, this.item}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () => {G.pushNamed('/expert_detail', arguments: item)},
        child: Container(
          padding: EdgeInsets.fromLTRB(0, 20, 0, 20),
          decoration: BoxDecoration(
            border: G.borderBottom(),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ClipOval(
                child: Image.network(
                  '${item.photo}',
                  width: 80.w,
                  height: 80.w,
                  fit: BoxFit.cover,
                ),
              ),
              Expanded(
                child: Container(
                  padding: EdgeInsets.only(left: 12),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Text(
                            '${item.name}',
                            style: mainTitleStyle,
                          ),
                          Container(
                            padding: EdgeInsets.only(left: 28.w),
                            child: Text(
                              '${item.agent}',
                              overflow: TextOverflow.ellipsis,
                              maxLines: 1,
                              style: TextStyle(
                                  color: rgba(51, 51, 51, 1), fontSize: 26.nsp),
                            ),
                          )
                        ],
                      ),
                      Padding(
                        padding: EdgeInsets.fromLTRB(0, 12, 0, 10),
                        child: Row(
                          children: [
                            LabelProject(name: item.researchArea ?? item.title),
                            // LabelProject(name: '医疗'),
                          ],
                        ),
                      ),
                      Text(
                        '${item.name}，${item.title}${item.fruit == null ? '' : '，' + item.fruit}',
                        style: TextStyle(
                            color: rgba(136, 135, 135, 1), fontSize: 28.nsp),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ));
  }
}

class ExpertList extends StatefulWidget {
  final isRecommend;
  const ExpertList({
    Key key,
    this.isRecommend,
  }) : super(key: key);
  @override
  _ExpertListState createState() => _ExpertListState();
}

class _ExpertListState extends State<ExpertList>
    with AutomaticKeepAliveClientMixin {
  List<HomeExpertData> experts = [];

  @override
  void initState() {
    super.initState();
    this.setupData();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      child: Container(
          color: Colors.white,
          padding: EdgeInsets.all(10),
          child: widget.isRecommend == true && experts.length > 2
              ? Column(
                  children: experts
                      .getRange(0, 3)
                      .map((item) => Expert(item: item))
                      .toList())
              : Column(
                  children:
                      experts.map((item) => Expert(item: item)).toList())),
    );
  }

  @override
  bool get wantKeepAlive => true;

  void setupData() async {
    G.showLoading(
      status: '加载中...',
    );
    var res = await G.req.home.expertList();
    var resData = res.data['data']['list'];
    resData.forEach((e) {
      HomeExpertData expert = HomeExpertData.fromJson(e);
      experts.add(expert);
    });
    this.setState(() {});
    G.dismissLoading();
  }
}
